package artifacts;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.NodeList;

import cartago.Artifact;
import cartago.OPERATION;


public class ParserXML extends Artifact{
	
	@OPERATION public void init()  {}
	
	org.w3c.dom.Document documento;
	
	public ParserXML(){
		
	}
	
	public String cargarXML(String file)
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		
		
		String kitId = "";
		try {
			builder = factory.newDocumentBuilder();
			documento = builder.parse(new File(file));
			kitId = documento.getElementsByTagName("kitId").item(0).getTextContent();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		return kitId;
	}
	

	
	public String getNextDocument(int index)
	{
		String result = "";
		NodeList documentos = documento.getElementsByTagName("requiredDocument");
		
		if(documentos.getLength() > index)
		{
			result = documentos.item(index).getChildNodes().item(0).getTextContent();
		}
		return result;
		
	}
	
	public String getNextDomain(int index)
	{
		String result = "";
		NodeList documentos = documento.getElementsByTagName("requiredDocument");
		
		if(documentos.getLength() > index)
		{
			result = documentos.item(index).getChildNodes().item(1).getTextContent();
		}
		return result;
		
	}
	
	
	public static void main(String[] args) {
		ParserXML parser = new ParserXML();
		parser.cargarXML("c:/kit.xml");
		parser.getNextDocument(0);
	}
	

}
